var Customer = function(customers) {
	this.customers = customers;
}
function createCustomerModel(cus) {
	var customer = cus.getElementsByTagName("User");
	var arrCus = [];
	var customerId;
	var customerUsername;
	var customerLName = "";
	var customerFName = "";
	var customerPhone = "";
	var customerAddress = "";
	var customerIsBan;
	var customerEmail;

	for (var i = 0; i < customer.length; i++) {
		customerId = customer[i].getAttribute("userId");
		customerUsername = customer[i].getElementsByTagName("userName")[0].childNodes[0].nodeValue;
		if (customer[i].getElementsByTagName("lastName")[0] != null) {
			customerLName = customer[i].getElementsByTagName("lastName")[0].childNodes[0].nodeValue;
		}

		if (customer[i].getElementsByTagName("firstName")[0] != null) {
			customerFName = customer[i].getElementsByTagName("firstName")[0].childNodes[0].nodeValue;
		}

		if (customer[i].getElementsByTagName("phone")[0] != null) {
			customerPhone = customer[i].getElementsByTagName("phone")[0].childNodes[0].nodeValue;
		}

		if (customer[i].getElementsByTagName("address")[0] != null) {
			customerAddress = customer[i].getElementsByTagName("address")[0].childNodes[0].nodeValue;
		}
		customerIsBan = customer[i].getElementsByTagName("isBanned")[0].childNodes[0].nodeValue;
		customerEmail = customer[i].getElementsByTagName("email")[0].childNodes[0].nodeValue;

		arrCus[i] = {
			"customerId" : customerId,
			"customerUsername" : customerUsername,
			"customerLName" : customerLName,
			"customerFName" : customerFName,
			"customerPhone" : customerPhone,
			"customerAddress" : customerAddress,
			"customerIsBan" : customerIsBan,
			"customerEmail" : customerEmail,
		}
	}

	return new Customer(arrCus);
}

var cusModel = function(cusId, cusName, cusLName, cusFName, cusEmail, cusAddr, cusPhone, banned) {
	this.cusId = cusId;
	this.cusName = cusName;
	this.cusLName = cusLName;
	this.cusFName = cusFName;
	this.cusEmail = cusEmail;
	this.cusAddr = cusAddr;
	this.cusPhone = cusPhone;
	this.banned = banned;
}
function showManageCustomerPage(arrCus) {
	var key = "customer";
	addActive(key);
	var rootNode = $id("loadCustomer");
	rootNode.innerHTML = "";
	if (arrCus.length > 0) {
		//create new table
		var nodeTable = createTable();
		nodeTable.setAttribute("id", "tableCustomer");
		var nodeThId = createNode("th");
		var idTh = createTextNode("Mã nhân viên");
		nodeThId.appendChild(idTh);
		var nodeThUName = createNode("th");
		var uNameTh = createTextNode("Tài khoản");
		nodeThUName.appendChild(uNameTh);
		var nodeThLName = createNode("th");
		var lNameTh = createTextNode("Họ");
		nodeThLName.appendChild(lNameTh);
		var nodeThFName = createNode("th");
		var fNameTh = createTextNode("Tên");
		nodeThFName.appendChild(fNameTh);
		var nodeThEmail = createNode("th");
		var emailTh = createTextNode("E-mail");
		nodeThEmail.appendChild(emailTh);
		var nodeThAdd = createNode("th");
		var addTh = createTextNode("Địa chỉ");
		nodeThAdd.appendChild(addTh);
		var nodeThPhone = createNode("th");
		var phoneTh = createTextNode("Điện thoại");
		nodeThPhone.appendChild(phoneTh);
		var nodeThBan = createNode("th");
		var banTh = createTextNode("Banned");
		nodeThBan.appendChild(banTh);
		var nodeThUpdate = createNode("th");
		var nodeThDelete = createNode("th");

		nodeTable.appendChild(nodeThId);
		nodeTable.appendChild(nodeThUName);
		nodeTable.appendChild(nodeThLName);
		nodeTable.appendChild(nodeThFName);
		nodeTable.appendChild(nodeThEmail);
		nodeTable.appendChild(nodeThAdd);
		nodeTable.appendChild(nodeThPhone);
		nodeTable.appendChild(nodeThBan);
		nodeTable.appendChild(nodeThUpdate);
		nodeTable.appendChild(nodeThDelete);

		var nodeTr = null;
		var customer = null;
		var nodeId = null;
		var nodeUserName = null;
		var nodeLName = null;
		var nodeFName = null;
		var nodeEmail = null;
		var nodeAdd = null;
		var nodePhone = null;
		var nodeBan = null;
		var nodeTdUpdate = null;
		var nodeDivUpdate = null;
		var nodeUpdate = null;
		var nodeTdDelete = null;
		var nodeDivDelete = null;
		var nodeDelete = null;

		var length = arrCus.length;

		for (var i = 0; i < length; i++) {
			customer = arrCus[i];
			nodeTr = createNode("tr");
			nodeTr.setAttribute("id", "cusIdInRow" + customer.customerId);

			nodeId = createNode("td");
			var id = createTextNode(customer.customerId);
			nodeId.appendChild(id);

			nodeUserName = createNode("td");
			var uname = createTextNode(customer.customerUsername);
			nodeUserName.appendChild(uname);

			nodeLName = createNode("td");
			var lname = createTextNode(customer.customerLName);
			nodeLName.appendChild(lname);

			nodeFName = createNode("td");
			var fname = createTextNode(customer.customerFName);
			nodeFName.appendChild(fname);

			nodeEmail = createNode("td");
			var email = createTextNode(customer.customerEmail);
			nodeEmail.appendChild(email);

			nodeAdd = createNode("td");
			var addr = createTextNode(customer.customerAddress);
			nodeAdd.appendChild(addr);

			nodePhone = createNode("td");
			var phone = createTextNode(customer.customerPhone);
			nodePhone.appendChild(phone);

			nodeBan = createNode("td");
			var ban = createTextNode(customer.customerIsBan);
			nodeBan.appendChild(ban);

			//create node update
			nodeTdUpdate = createNode("td");
			nodeDivUpdate = createDiv();
			nodeTdUpdate.addEventListener("click", function(event) {
				var currentRow = $id(event.currentTarget.parentNode.id);
				var cels = currentRow.getElementsByTagName("td");
				var cusId = cels[0].innerText;
				var cusName = cels[1].innerText;
				var cusLName = cels[2].innerText;
				var cusFName = cels[3].innerText;
				var cusEmail = cels[4].innerText;
				var cusAddr = cels[5].innerText;
				var cusPhone = cels[6].innerText;
				var banned = cels[7].innerText;

				var model = new cusModel(cusId, cusName, cusLName, cusFName, cusEmail, cusAddr, cusPhone, banned);
				showPage("content-updateCustomer");
				createUpdateCusView(model.cusId, model.cusName, model.cusLName, model.cusFName, model.cusEmail, model.cusAddr, model.cusPhone, model.banned);

			});
			nodeUpdate = createNode("img");
			nodeUpdate.setAttribute("src", "Image/update.png");
			nodeUpdate.style.cursor = "pointer";
			nodeDivUpdate.appendChild(nodeUpdate);
			nodeTdUpdate.appendChild(nodeDivUpdate);

			//create node delete
			nodeTdDelete = createNode("td");
			nodeDivDelete = createDiv();
			nodeDelete = createNode("img");
			nodeDelete.setAttribute("src", "Image/delete_icon.png");
			nodeDelete.style.cursor = "pointer";
			nodeTdDelete.addEventListener("click", function(event) {
				var row = $id(event.currentTarget.parentNode.id);
				var cels = row.getElementsByTagName("td");
				var uId = cels[0].innerText;
				var uName = cels[1].innerText;
				var r = confirm("Bạn có muốn xóa khách hàng có tài khoản " + uName + "?");

				if (r == true) {
					delCustomer(uId, uName);
				}
			});
			nodeDivDelete.appendChild(nodeDelete);
			nodeTdDelete.appendChild(nodeDivDelete);

			nodeTr.appendChild(nodeId);
			nodeTr.appendChild(nodeUserName);
			nodeTr.appendChild(nodeLName);
			nodeTr.appendChild(nodeFName);
			nodeTr.appendChild(nodeEmail);
			nodeTr.appendChild(nodeAdd);
			nodeTr.appendChild(nodePhone);
			nodeTr.appendChild(nodeBan);
			nodeTr.appendChild(nodeTdUpdate);
			nodeTr.appendChild(nodeTdDelete);

			nodeTable.appendChild(nodeTr);

		}
		rootNode.appendChild(nodeTable);
		var divPaging = createDiv();
		divPaging.setAttribute("id", "pageCustomer");
		divPaging.setAttribute("class", "paging");
		var script = createNode("script");
		script.setAttribute("type", "text/javascript");
		script.innerHTML = "var pager = new Pager('tableCustomer', 10); pager.init(); pager.showPageNav('pager', 'pageCustomer'); pager.showPage(1);";
		rootNode.appendChild(divPaging);
		rootNode.appendChild(script);
	}
}

function callServiceCustomer() {

	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageCustomer",
		success : function(data, xhr) {
			showPage("content-manageCustomer");
			var model = createCustomerModel(data);
			if (model.customers.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageCustomerPage(model.customers);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function delCustomer(uId, uName) {
	ajax.del({
		url : "http://localhost:8080/book-service/staff/manageCustomer",
		param : {
			"txtCusId" : uId
		},
		success : function() {
			alert("Xóa khách hàng có tài khoản " + uName + " thành công");
			showPage("content-manageCustomer");
			callServiceCustomer();
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

function searchCustomer() {
	var txtSearch = document.getElementById("txtSearchCustomer").value;
	ajax.get({
		url : "http://localhost:8080/book-service/staff/manageCustomer/searchCustomer",
		param : {
			"txtUserName" : txtSearch
		},
		success : function(data, xhr) {
			var model = createCustomerModel(data);
			if (model.customers.length <= 0) {
				alert("Không có kết quả!");
			}
			showManageCustomerPage(model.customers);
		},
		failure : function(xhr) {
			if (xhr.status == 401) {
				alert("Bạn không có quyền truy cập vào trang này!\n Xin mời đăng nhập lại");
				doRequestFail();
			} else {
				alert("Có lỗi xảy ra. Xin thử lại!");
			}
		},
		abort : function(xhr) {
			callback.abort(xhr);
		}
	});
}

